package cn.hcbycloud.code.utils;

import cn.hcbycloud.code.model.TbDatabaseConfEntity;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.builder.ConfigBuilder;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;

import java.util.List;

/**
 * 数据源工具
 *
 * @author: WangHui
 * @date: 2020-02-05 17:54:12
 */
public class DataSourceUtils {
    /**
     * 获取数据源表信息
     * @param tbDatabaseConf
     * @return
     */
    public static List<TableInfo> getTableList(TbDatabaseConfEntity tbDatabaseConf) {
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setDbType(DbType.getDbType("mysql"));
        dataSourceConfig.setUrl(tbDatabaseConf.getUrl());
        dataSourceConfig.setSchemaName(tbDatabaseConf.getDbName());
        dataSourceConfig.setDriverName("com.mysql.jdbc.Driver");
        dataSourceConfig.setUsername(tbDatabaseConf.getUserName());
        dataSourceConfig.setPassword(tbDatabaseConf.getPassword());
        ConfigBuilder config = new ConfigBuilder(null, dataSourceConfig, null, null, null);
        return config.getTableInfoList();
    }
}
